package com.sk.sourcecircle.activity.home.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dyhdyh.widget.loading.dialog.LoadingDialog;
import com.sk.sourcecircle.R;
import com.sk.sourcecircle.adapter.MoreConsultAdapter;
import com.sk.sourcecircle.app.MyApplication;
import com.sk.sourcecircle.base.BaseActivity;
import com.sk.sourcecircle.callback.HandleDataCallBack;
import com.sk.sourcecircle.entity.NoticeInfoBean;
import com.sk.sourcecircle.utils.AppApi;
import com.sk.sourcecircle.utils.MyUtils;
import com.sk.sourcecircle.view.EasyLoadingMoreView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by sk on 2018/1/1.
 */

public class LocalMoreConsultActivity extends BaseActivity implements BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.RequestLoadMoreListener, HandleDataCallBack {
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.data_ryc)
    RecyclerView dataRyc;
    private int page = 1;
    private int type;
    private int cateId = 0;

    private MoreConsultAdapter adapter;
    private List<NoticeInfoBean.DataBean> allDataList = new ArrayList<>();
    private LoadingDialog loadingDialog;
    private boolean isLoadMore = false;


    @Override
    protected int getContentView() {
        return R.layout.activity_more_local_ablum;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        tvTitle.setText(MyApplication.getInstance().getCityName()+"资讯");

        loadingDialog = MyUtils.getLoadingDialog(mContext, false);

        cateId = getIntent().getIntExtra("cate_id", 0);


        dataRyc.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new MoreConsultAdapter(R.layout.item_local_article, allDataList);
        dataRyc.setAdapter(adapter);
        EasyLoadingMoreView loadMoreView = new EasyLoadingMoreView();
        adapter.setLoadMoreView(loadMoreView);
        View emptyView = getLayoutInflater().inflate(R.layout.empty_view, null);
        adapter.setEmptyView(emptyView);
        adapter.setOnItemClickListener(this);
        adapter.setOnLoadMoreListener(this, dataRyc);

        loadingDialog.show();
        AppApi.getLocalConsult(cateId,MyApplication.getInstance().getCityId(), page, this, 0, this);
    }

    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

        Intent intent = new Intent(mContext, DetailsOfConsultationActivity.class);
        intent.putExtra("id", allDataList.get(position).getArticle_id());
        startActivity(intent);
        int show = allDataList.get(position).getShow();
        allDataList.get(position).setShow(show+1);
        adapter.notifyItemChanged(position);

    }

    @Override
    public void onLoadMoreRequested() {
        isLoadMore = true;
        page++;
        AppApi.getLocalConsult(cateId,MyApplication.getInstance().getCityId(), page, this, 0, this);
    }

    @Override
    public void handlerSuccessData(String result, int tag) {
        loadingDialog.cancelDialog();
        NoticeInfoBean eventListBean = JSON.parseObject(result, NoticeInfoBean.class);
        List<NoticeInfoBean.DataBean> dataBeanList = eventListBean.getData();
        allDataList.addAll(dataBeanList);
        if (allDataList.size() < 10) {
            adapter.setEnableLoadMore(false);
        } else {
            adapter.setEnableLoadMore(true);
        }
        if (isLoadMore) {
            adapter.loadMoreComplete();
            if (dataBeanList.size() == 0) {
                adapter.loadMoreEnd();
            }
            isLoadMore = false;
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void handlerFailData(String failString, int tag) {
        loadingDialog.cancelDialog();
    }

    @Override
    public void handleErrorData(String error, int tag) {
        loadingDialog.cancelDialog();
    }
}
